# -*- coding: utf-8 -*-

"""
@author: Laowang
@contact: QQ:1125564921
@Created on: 2022/6/20
@Remark: 
"""

import json
from sqlalchemy import Column, String, DateTime, func, Text, Boolean, Integer

from framework_strategy.gopy.driver.core import Base, engine


class BaseModel(Base):

    __abstract__ = True

    create_time = Column(DateTime, comment="创建时间", server_default=func.now())
    updatetime = Column(DateTime, server_default=func.now(),
                        onupdate=func.now(), comment="修改时间")

    def __format__(self, __format_spec: str) -> str:
        if not __format_spec:
            return json.dumps(self.__dict__)
        return super().__format__(__format_spec)


class StrategyModel(BaseModel):
    """
    strategy meta data
    """

    __tablename__ = "gopy_strategy_info"

    id = Column(Integer, primary_key=True)
    app_name = Column(String(100), comment="策略名称")
    user_name = Column(String(100), comment="策略人员")
    note = Column(Text, comment="策略备注", nullable=True)
    is_delete = Column(Boolean, default=False)
    status = Column(String(50), comment="策略状态", nullable=True)

    def __repr__(self) -> str:
        fmt = "<{0.__class__.__name__!r} (id={0.id!r}, app_name={0.app_name!r})>".format(
            self)
        return fmt


# 日期
# 平台
# 类型
# 价格
# 数量
# 信息
class StrategyOrderLine(BaseModel):
    """
    strategy run orderline
    """
    DROP = ["uuid", "user_name", "app_name", "note", "detail", "is_contrace", "status"]

    __tablename__ = "gopy_strategy_orderline"

    id = Column(Integer, primary_key=True)
    uuid = Column(String(50), comment="uuid")
    orderId =  Column(String(50), comment="订单号", nullable=True)
    exchange = Column(String(50), comment="平台名称")
    market = Column(String(50), comment="市场名", nullable=True)
    side = Column(String(10), comment="操作方向", nullable=True)
    price = Column(String(50), comment="价格", nullable=True)
    number = Column(String(50), comment="数量", nullable=True)
    app_name = Column(String(100), comment="策略名称", nullable=True)
    detail = Column(Text, comment="操作详情", nullable=True)
    note = Column(Text, comment="备注信息", nullable=True)
    user_name = Column(String(100), comment="策略人员", nullable=True)
    response_content = Column(Text, comment="响应报文", nullable=True)
    is_contract = Column(Boolean, comment="是否是合约", default=False)
    status = Column(Boolean, comment="是否操作成功", default=False)

    def __repr__(self) -> str:
        fmt = "<{0.__class__.__name__!r}(id={0.exchange!r}, app_name={0.side!r})>".format(
            self)
        return fmt

    def __format__(self, __format_spec: str) -> str:

        fmt = "平台：{0.exchange!r} 市场: {0.market!r} 价格: {0.price!r} {0.side!r} 数量 {0.number!r}".format(
            self)
        if not __format_spec:
            return fmt
        return super().__format__(__format_spec)


Base.metadata.create_all(engine)
